<html>
<head>
    <meta charset="UTF-8">
</head>
<body>
    <h1>维纳过程</h1>
    <p>平均值是：<span id="avgvalue"></span></p>
    <p>差分平均值是：<span id="avgdiffvalue"></span><br>最大和最小差分：<span id="maxdiff"></span>,<span id="mindiff"></span></p>
    <p>分布：<br><canvas id="distribution" width="800" height="400"><canvas></p>
    <script>
        function normalDistribution(mean, standardDeviation) {
            // Box-Muller
            let U1 = 1 - Math.random();
            let U2 = 1 - Math.random();
            let R = Math.sqrt(-2 * Math.log(U2));
            let THETA = 2 * Math.PI * U1;
            let Z = R * Math.cos(THETA);
            return mean + (Z * standardDeviation);
        }
        function wienerProcess(x, mean, standardDeviation) {
            return x + normalDistribution(mean, standardDeviation);
        }
        function draw(context2d, width, height, arrayDraw) {
            let dx = width / arrayDraw.length;
            let max = Math.max(...arrayDraw);
            let min = Math.min(...arrayDraw);
            let hei = max - min;
            context2d.fillStyle = "#FFFFFF";
            context2d.fillRect(0, 0, width, height);
            context2d.lineWidth = 2;
            context2d.strokeStyle = "black";
            for (let i in arrayDraw) {
                if (i == 0) {
                    context2d.moveTo(i * dx, height * (1 - (arrayDraw[i] - min) / hei));
                } else {
                    context2d.lineTo(i * dx, height * (1 - (arrayDraw[i] - min) / hei));
                }
            }
            context2d.stroke();
        }

        let sequence = [0];
        for (let i = 1; i < 50000; i++) {
            sequence[i] = wienerProcess(sequence[i - 1], 0, 1);
        }
        // 统计平均值
        let avg = 0;
        let sum = 0;
        for (let i = 0; i < sequence.length; i++) {
            sum += sequence[i];
        }
        avg = sum / sequence.length;
        document.getElementById("avgvalue").innerText = avg;

        // 统计增量的平均值，最大差分，和最小差分
        avg = 0;
        sum = 0;
        let maxDiff = sequence[1] - sequence[0];
        let minDiff = maxDiff;
        let temp;
        for (let i = 1; i < sequence.length; i++) {
            temp = sequence[i] - sequence[i - 1];
            sum += temp;
            if (temp > maxDiff) {
                maxDiff = temp;
            }
            if (temp < minDiff) {
                minDiff = temp;
            }
        }
        avg = sum / (sequence.length - 1);
        document.getElementById("avgdiffvalue").innerText = avg;
        document.getElementById("maxdiff").innerText = maxDiff;
        document.getElementById("mindiff").innerText = minDiff;

        // 统计分布特点
        let distributionsLength = 100;
        let distributions = [];
        let distributionsInscr = (maxDiff - minDiff) / distributionsLength;
        for (let i = 0; i < distributionsLength; i++) {
            var downLimit = minDiff + i * distributionsInscr;
            var topLimit = minDiff + (i + 1) * distributionsInscr;
            sum = 0;
            for (let j = 1; j < sequence.length; j++) {
                temp = sequence[j] - sequence[j - 1];
                if (temp > downLimit && temp <= topLimit) {
                    sum++;
                }
            }
            distributions.push(sum);
        }
        draw(document.getElementById("distribution").getContext("2d"), 800, 400, distributions);
    </script>
</body>
<html>